#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define HOST "www.baidu.com"
#define PORT "80"

void mainCRTStartup()
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    {
        WSADATA wsaData;
        WSAStartup(MAKEWORD(2,2), &wsaData);
    }

    struct addrinfo hints =
    {
        0,
        AF_UNSPEC,
        SOCK_STREAM,
        IPPROTO_TCP,
        0,
        NULL,
        NULL,
        NULL,
    };

    struct addrinfo *result = NULL;
    getaddrinfo(HOST, PORT, &hints, &result);
    SOCKET sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    connect(sock, result->ai_addr, (int)result->ai_addrlen);
    freeaddrinfo(result);

    #define sendbuf "GET / HTTP/1.1\r\n" \
                    "Host: " HOST "\r\n" \
                    "Accept-Encoding: identity\r\n" \
                    "User-Agent: test\r\n" \
                    "Connection: close\r\n" \
                    "\r\n"
    send(sock, sendbuf, sizeof sendbuf - 1, 0);

    for (;;)
    {
        char buf[32];
        int r = recv(sock, buf, sizeof buf, 0);
        if (r < 1)
            break;
        WriteConsoleA(hStdOut, buf, r, NULL, NULL);
    }

    closesocket(sock);
    WSACleanup();

    ExitProcess(0);
}